package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.MarketSystem;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.configVo.ConfigMallVo;
import com.cskaoyan.bean.configVo.ExpressMallVo;
import com.cskaoyan.bean.configVo.OrderMallVo;
import com.cskaoyan.bean.configVo.WxMallVo;
import com.cskaoyan.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//发现post可以放在一个里面

/**
 * @Author Austin Hsu
 * @Date 2022/9/7 15:08
 */
@RestController
@RequestMapping("admin/config")
public class ConfigController {

    @Autowired
    ConfigService configService;



    @GetMapping("wx")
    public BaseRespVo wx() {
        List<MarketSystem> marketSystems = configService.wx();
        WxMallVo data = new WxMallVo();
        for (MarketSystem marketSystem : marketSystems) {
            if ("market_wx_index_new".equals(marketSystem.getKeyName())) {
                data.setMarket_wx_index_new(marketSystem.getKeyValue());
            }
            if ("market_wx_index_topic".equals(marketSystem.getKeyName())) {
                data.setMarket_wx_index_topic(marketSystem.getKeyValue());
            }
            if ("market_wx_share".equals(marketSystem.getKeyName())) {
                data.setMarket_wx_share(marketSystem.getKeyValue());
            }
            if ("market_wx_index_brand".equals(marketSystem.getKeyName())) {
                data.setMarket_wx_index_brand(marketSystem.getKeyValue());
            }
            if ("market_wx_catlog_goods".equals(marketSystem.getKeyName())) {
                data.setMarket_wx_catlog_goods(marketSystem.getKeyValue());
            }
            if ("market_wx_catlog_list".equals(marketSystem.getKeyName())) {
                data.setMarket_wx_catlog_list(marketSystem.getKeyValue());
            }
            if ("market_wx_index_hot".equals(marketSystem.getKeyName())) {
                data.setMarket_wx_index_hot(marketSystem.getKeyValue());
            }
        }

        return BaseRespVo.ok2(data);
    }

    @PostMapping("wx")
    public BaseRespVo wx2(@RequestBody Map<String,String> map){
        configService.wx2(map);
        return BaseRespVo.ok3();
    }

    @GetMapping("order")
    public BaseRespVo order() {
        List<MarketSystem> marketSystems = configService.order();
        OrderMallVo data = new OrderMallVo();
        for (MarketSystem marketSystem : marketSystems) {
            if ("market_order_unpaid".equals(marketSystem.getKeyName())) {
                data.setMarket_order_unpaid(marketSystem.getKeyValue());
            }
            if ("market_order_unconfirm".equals(marketSystem.getKeyName())) {
                data.setMarket_order_unconfirm(marketSystem.getKeyValue());
            }
            if ("market_order_comment".equals(marketSystem.getKeyName())) {
                data.setMarket_order_comment(marketSystem.getKeyValue());
            }
        }

        return BaseRespVo.ok2(data);
    }

    @PostMapping("order")
    public BaseRespVo order2(@RequestBody Map<String,String> map){
        configService.order2(map);
        return BaseRespVo.ok3();
    }

    /**
     * 接口69.1 复用68
     * @return
     */
    @GetMapping("express")
    public BaseRespVo express(){
        List<MarketSystem> marketSystems=configService.express();
        ExpressMallVo data=new ExpressMallVo();
        for (MarketSystem marketSystem : marketSystems) {
            if ("market_express_freight_min".equals(marketSystem.getKeyName())){                  //如果数据库的key_name是latitude
                data.setMarket_express_freight_min(marketSystem.getKeyValue());            //VO的该属性得到latitude的key_value
            }
            if ("market_express_freight_value".equals(marketSystem.getKeyName())){
                data.setMarket_express_freight_value(marketSystem.getKeyValue());
            }
        }

        return BaseRespVo.ok2(data);

    }

    @PostMapping("express2")
    public BaseRespVo express2(@RequestBody Map<String,String> map){
        configService.express2(map);
        return BaseRespVo.ok3();
    }
    /**
     * 接口68.2 提交改变
     * 1.Map map传入Service中
     *          修改语句：updateSeletive set key_value #{map.value} -- where key_name=#{map.key} 循环
     * @param map
     * @return
     */
    @PostMapping("mall")
    public BaseRespVo mall2(@RequestBody Map<String,String> map){
        configService.mall2(map);
        return BaseRespVo.ok3();
    }

    /**
     * 接口68.1
     * 1.查询得到List<MarketSystem> list
     *                     查询语句：select * from market_system where id in (3,7,9,13,14,16);
     * 2.ConfigMall congfigmall接收 遍历list
     *                     判断语句：if key_name=xx 就configmall.set(xx)=list元素.get(key_value)
     *
     */
    @GetMapping("mall")
    public BaseRespVo mall(){
        List<MarketSystem> marketSystems=configService.mall();
        ConfigMallVo data=new ConfigMallVo();
        for (MarketSystem marketSystem : marketSystems) {
                if ("market_mall_latitude".equals(marketSystem.getKeyName())){                  //如果数据库的key_name是latitude
                    data.setMarket_mall_latitude(marketSystem.getKeyValue());            //VO的该属性得到latitude的key_value
                }
                if ("market_mall_name".equals(marketSystem.getKeyName())){
                    data.setMarket_mall_name(marketSystem.getKeyValue());
                }
                if ("market_mall_qq".equals(marketSystem.getKeyName())){
                    data.setMarket_mall_qq(marketSystem.getKeyValue());
                }
                if ("market_mall_longitude".equals(marketSystem.getKeyName())){
                    data.setMarket_mall_longitude(marketSystem.getKeyValue());
                }
                if ("market_mall_phone".equals(marketSystem.getKeyName())){
                    data.setMarket_mall_phone(marketSystem.getKeyValue());
                }
                if ("market_mall_address".equals(marketSystem.getKeyName())){
                    data.setMarket_mall_address(marketSystem.getKeyValue());
                }
        }

        return BaseRespVo.ok2(data);
    }

}
